package com.fr.privilege.providers;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.StringUtils;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.privilege.authentication.Authentication;
import com.fr.privilege.authority.Authority;
import com.fr.util.Utils;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class HttpAuthenticationProvider extends AbstractAuthenticationProvider
{
  private String url;

  public String getUrl()
  {
    return this.url;
  }

  public void setUrl(String paramString)
  {
    this.url = paramString;
  }

  public boolean authenticate(Authentication paramAuthentication)
    throws Exception
  {
    if (this.url == null)
      throw new Exception(Inter.getLocText("Privilege-The_Url_Can_Not_Be_Null"));
    HttpClient localHttpClient = new HttpClient();
    PostMethod localPostMethod = new PostMethod(this.url);
    String str1 = paramAuthentication.getPrincipal().toString();
    String str2 = paramAuthentication.getCredentials().toString();
    localPostMethod.addParameter(new NameValuePair("fr_username", str1));
    localPostMethod.addParameter(new NameValuePair("fr_password", str2));
    int i = localHttpClient.executeMethod(localPostMethod);
    if (i != 200)
      throw new Exception("Method failed: " + localPostMethod.getStatusLine());
    try
    {
      InputStream localInputStream = localPostMethod.getResponseBodyAsStream();
      if (localInputStream == null)
      {
        int j = 0;
        return j;
      }
      ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
      Utils.copyBinaryTo(localInputStream, localByteArrayOutputStream);
      byte[] arrayOfByte1 = localByteArrayOutputStream.toByteArray();
      int k = arrayOfByte1.length;
      int l = 0;
      for (int i1 = 0; i1 < k; ++i1)
        if ((arrayOfByte1[i1] != 10) && (arrayOfByte1[i1] != 13))
          ++l;
      byte[] arrayOfByte2 = new byte[l];
      int i2 = 0;
      for (int i3 = 0; i2 < k; ++i3)
      {
        if ((arrayOfByte1[i2] != 10) && (arrayOfByte1[i2] != 13))
          arrayOfByte2[i3] = arrayOfByte1[i2];
        ++i2;
      }
      String str3 = new String(arrayOfByte2, "GBK");
      FRContext.getLogger().log(Level.INFO, str3);
      if ("false".equalsIgnoreCase(str3.trim()))
      {
        i3 = 0;
        return i3;
      }
      paramAuthentication.setAuthenticated(true);
      String[] arrayOfString = str3.split(",");
      Authority[] arrayOfAuthority = new Authority[arrayOfString.length];
      for (int i4 = 0; i4 < arrayOfString.length; ++i4)
        arrayOfAuthority[i4] = new Authority(arrayOfString[i4]);
      paramAuthentication.setAuthorities(arrayOfAuthority);
      i4 = 1;
      return i4;
    }
    finally
    {
      localPostMethod.releaseConnection();
    }
  }

  public void readJSON(JSONObject paramJSONObject)
    throws JSONException
  {
    super.readJSON(paramJSONObject);
    if (paramJSONObject.has("url"))
      setUrl(paramJSONObject.getString("url"));
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str = paramXMLableReader.getTagName();
      if ("URL".equals(str))
        this.url = paramXMLableReader.getElementValue();
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    if (StringUtils.isNotBlank(this.url))
      paramXMLPrintWriter.startTAG("URL").textNode(this.url).end();
  }
}